/**
 * User: liuzj
 * Date: 15-11-13
 * Time: 上午5:10
 * 车辆信息infowindow
 */

define(
    [
        "require",
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/_base/array",
        "dojo/dom-attr",
        "dojo/dom-class",
        "dojo/dom-construct",
        "dojo/_base/html",
        "dojo/dom-style",
        "dojo/on",
        "dojo/query",
        "dojo/topic",
        "gistech/widgets/BaseWidget",
        "gistech/util/infoUtil",
        "gistech/util/ajaxUtil",
        "esri/InfoWindowBase",
        "dojo/text!./templates/Template.html"
    ],
    function (require, declare, lang, array, domAttr, domClass, domConstruct, html, domStyle, on, query, topic, BaseWidget, infoUtil, ajaxUtil, InfoWindowBase, template) {

        return declare([BaseWidget], {

            templateString:template,

            constructor:function (args) {
                this.container = args.container;
                this.vehicleGraphic=args.vehicleGraphic;
                this.peopleGraphic =args.peopleGraphic;
            },

            postMixInProperties:function () {
                this.inherited(arguments);
            },

            postCreate:function () {
                this.init();
                this.addEvents();
                this.inherited(arguments);
            },

            startup:function () {
                this.inherited(arguments);
            },

            destroy:function () {
                this.inherited(arguments);
            },
            
            /**
             * 初始化
             */
            init:function () {
                domConstruct.place(this.domNode, this.container);
                
                this._initTab1(this.vehicleGraphic);
                this._initTab2(this.peopleGraphic);
                

            },
            
            /**
             * 添加事件
             */
            addEvents:function () {
            	query("ul#querytabs li a").forEach(lang.hitch(this,function(node,index){
                	dojo.connect(node,"click",lang.hitch(this,function (){
                		if (index==0){
                			domStyle.set(query("div.container table.tab1")[0], "display", "block");
                			domStyle.set(query("div.container table.tab2")[0], "display", "none");
                			
                			query("ul#querytabs li a").forEach(lang.hitch(this,function(temnode,index_a){
      			    		     if (index_a==0) domClass.add(temnode, "tab_chooseon");
      			    		     else if(index_a==1) domClass.remove(temnode, "tab_chooseon");
  			    	        }));
                		}else if(index==1){
                			
                			domStyle.set(query("div.container table.tab1")[0], "display", "none");
                			domStyle.set(query("div.container table.tab2")[0], "display", "block");
                			
                			query("ul#querytabs li a").forEach(lang.hitch(this,function(temnode,index_a){
     			    		     if (index_a==1) domClass.add(temnode, "tab_chooseon");
     			    		     else if(index_a==0) domClass.remove(temnode, "tab_chooseon");
 			    	        }));
                		}
                	}));
                }))
            },
            
            /*
             * 初始化第一个tab页，车辆信息
             * */
            _initTab1:function(){
            	var hc = 0;
            	var bgc = 0;
            	var cyc = 0;
            	for(var i=0;i<this.vehicleGraphic.length;i++){
            		if(this.vehicleGraphic[i].attr.vtype=="货车"){
            			hc++;
            		}else if(this.vehicleGraphic[i].attr.vtype=="办公车"){
            			bgc++;
            		}else if(this.vehicleGraphic[i].attr.vtype=="厨演车"){
            			cyc++;
            		}
            	}
            	
            	var html='<tr><td style="width:100px">总数</td><td style="width:110px">'+this.vehicleGraphic.length+'</td></tr>'+
            		     '<tr><td style="width:100px">货车</td><td style="width:110px">'+hc+'</td></tr>'+
                         '<tr><td>办公车</td><td>'+bgc+'</td></tr>'+
                         '<tr><td>厨演车</td><td>'+cyc+'</td></tr>';
            	query("div.container table.tab1")[0].innerHTML=html;
            },
            
            /*
             * 初始化第一个tab页，车辆信息
             * */
            _initTab2:function(){
            	var UserType = [];
            	
            	for(var i=0;i<this.peopleGraphic.length;i++){
            		UserType.push(this.peopleGraphic[i].attributes.UserType);
            	}
            	UserType = this._unique(UserType);//去掉重复的种类
            	var count = [];
            	
            	for(var i=0;i<UserType.length;i++){
            		count[i] = 0;
            		for(var j=0;j<this.peopleGraphic.length;j++){
                		if(this.peopleGraphic[j].attributes.UserType==UserType[i]){
                			count[i]++;
                		}
                	}
            	}
            	
            	var html = '<tr><td style="width:100px">总数</td><td style="width:110px">'+this.peopleGraphic.length+'</td></tr>';
            	for(var i=0;i<UserType.length;i++){
            		html += '<tr><td style="width:100px">'+UserType[i]+'</td><td style="width:110px">'+count[i]+'</td></tr>';
            	}

            	query("div.container table.tab2")[0].innerHTML=html;
            },
            
            /**
             *  去掉数组中的重复项
             * @param evt
             */
            _unique:function (arr) {
                var result = [], hash = {};
                for (var i = 0, elem; (elem = arr[i]) != null; i++) {
                    if (!hash[elem]) {
                        result.push(elem);
                        hash[elem] = true;
                    }
                }
                return result;
            },

        });
    });